package com.WOWTradeNews.tablet;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.WOWTradeNews.Loading;
import com.WOWTradeNewsTest.R;
import com.WOWTradeNews.DataBase.AsyncImageLoader;
import com.WOWTradeNews.DataBase.News;
import com.WOWTradeNews.DataBase.ViewNews;
import com.WOWTradeNews.DataBase.AsyncImageLoader.ImageCallback;

public class NewsListAdapter extends ArrayAdapter<News> {
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private int newsType;
    
    public NewsListAdapter(Activity activity, List<News> bonusList, ListView listView,int newsType) {
        super(activity, 0, bonusList);
        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();
        this.newsType=newsType;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        Activity activity = (Activity) getContext();

        // Inflate the views from XML
        View rowView = convertView;
        ViewNews viewNews;
        if (rowView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.market_research_list_item, null);
            viewNews = new ViewNews(rowView);
            rowView.setTag(viewNews);
        } else {
        	viewNews = (ViewNews) rowView.getTag();
        }
        News news = getItem(position);
        
        // Load the image and set it on the ImageView
        String imageFile = news.getImageUrl();
        ImageView imageView = viewNews.getImageView();
//        if(Loading.screenWidth >= 600 && Loading.screenWidth <800)
//        	imageView.setLayoutParams(new RelativeLayout.LayoutParams(160,100));
		imageView.setAdjustViewBounds(false);
		imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);        
        
        imageView.setTag(imageFile);        
        Bitmap cachedImage = asyncImageLoader.loadBitmap(imageFile, new ImageCallback() {
            public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                if (imageViewByTag != null) {
                	if(imageBitmap!=null){
                		imageViewByTag.setImageBitmap(imageBitmap);
                	}else{ 
                		switch (newsType) {
                		case 1:
                    		imageViewByTag.setImageResource(R.drawable.defaulticon1);
                    		break;
                		case 2:
                			imageViewByTag.setImageResource(R.drawable.defaulticon2);
                			break;
                		case 3:
                			imageViewByTag.setImageResource(R.drawable.defaulticon3);
                			break;
                		case 4:
                			imageViewByTag.setImageResource(R.drawable.defaulticon4);
                			break;
                		case 5:
                			imageViewByTag.setImageResource(R.drawable.defaulticon5);
                			break;
                		}
                	}
                }
            }
        });
        
		if (cachedImage == null) {
       		switch (newsType) {
    		case 1:
    			imageView.setImageResource(R.drawable.defaulticon1);
        		break;
    		case 2:
    			imageView.setImageResource(R.drawable.defaulticon2);
    			break;
    		case 3:
    			imageView.setImageResource(R.drawable.defaulticon3);
    			break;
    		case 4:
    			imageView.setImageResource(R.drawable.defaulticon4);
    			break;
    		case 5:
    			imageView.setImageResource(R.drawable.defaulticon5);
    			break;
    		}
		}else{			
			imageView.setImageBitmap(cachedImage);
		}

        TextView idText=viewNews.getIdText();
        idText.setText(news.getPK_ID());		
        TextView textView = viewNews.getTitleText();
        textView.setText(news.getTitle());
        TextView contentView = viewNews.getSummaryText();                
        contentView.setText(news.getSummary());

        return rowView;
    }    

}
